<template>
    <div>
        <el-container class="form-container">
            <el-card class="box-card" shadow="never">
                <div slot="header" class="clearfix">
                    <span>访问策略</span>
                    <el-button type="primary" size="mini" style="float:right;padding: 6px 9px;" @click="handlerSubmitForm">保存</el-button>
                </div>
                <div>
                    <el-form  :rules="rules"  ref="form"  :model="form" size="small" label-width="160px">
                        <el-row>
                        <el-col :span="24">
                            <el-form-item  label="名称" required prop="fdName">
                                <el-input class="input" v-model="form.fdName" required ></el-input>
                            </el-form-item>
                        </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item  label="rest应用Id" required prop="fdRestId">
                                    <el-input class="input" readonly="readonly" v-model="form.fdRestId" required  style="width:80%"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item  label="密钥" required prop="fdKey">
                                    <el-input class="input" readonly="readonly" v-model="form.fdKey" required  style="width:80%"/>&nbsp;&nbsp;&nbsp;
                                    <el-button type="primary" size="mini" @click="handlerGenerateToken">生成令牌</el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item  label="访问策略" required prop="fdAuthType">
                                    <el-radio-group v-model="form.fdAuthType" @change="handlerChangeAuthType">
                                        <el-radio v-for="item in authTypeEnums" :key="item.value" :label="item.value">{{item.text}}</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item  label="令牌有效时长（分钟）"  prop="fdAuthTime" v-show="form.fdAuthType === 'token'">
                                    <el-inputNumber class="input" v-model="form.fdAuthTime" ></el-inputNumber>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item  label="访问IP(请用;分隔)"  prop="fdAuthIp">
                                    <el-input  type="textarea" class="input" v-model="form.fdAuthIp"  ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item  label="备注"  prop="fdRemark">
                                    <el-input type="textarea" class="input" v-model="form.fdRemark"  ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </el-card>
        </el-container>
    </div>
</template>
<script>
import {edit,parentEdit} from '@/components/mixins/form'
export default {
    name: "sysRestPolicyEdit",
    components: {
    },
    data() {
        return {
            updateUrl:'/api/sys/rest/sys_rest_policy',
            fetchUrl:'/api/sys/rest/sys_rest_policy',
            fetchParentUrl:'/api/sys/rest/sys_rest_policy',
            form: {
                fdId:'',
                fdCreateTime:'',
                fdName:'',
                fdKey:'',
                fdRestId:'',
                fdAuthType:'anno',
                fdAuthIp:'',
                fdRemark:'',
                fdAuthTime:0
            },
            authTypeEnums:[
                {
                    value:"anno",
                    text:"匿名",
                },
                {
                    value:"token",
                    text:"令牌模式",
                }
            ],
            rules:{
                fdName :[{required: true, message: '请输入策略名称', trigger: 'blur'}],
                fdKey :[{required: true, message: '请输入密钥', trigger: 'blur'}],
                fdAuthType :[{required: true, message: '请输入访问策略', trigger: 'blur'}],
	        }
        }
    },
    methods:{
        handlerGenerateToken(){
            this.$fetch('/api/sys/rest/sys_rest_policy/generateToken',{}).then((res) => {
                if(res.status ===1){
                    this.form.fdKey = res.data.fdKey;
                    this.form.fdRestId = res.data.fdRestId;
                }
            })
        },
        handlerChangeAuthType(val){
            console.log(val);
            if(val !== 'token'){
               this.form.fdAuthTime=0; 
            }
        }
    },
    mixins:[edit,parentEdit]
}
</script>
<style scoped>
@import "~@/assets/css/form.css";
</style>